﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.Leo
{
    //分割领用修改
    public class SplitRequisUpdateHandler : IRequestHandler<SplitRequisUpdateCommend, Result>
    {
        private readonly IBaseRepository<SplitRequis> SplitRequis;

        public SplitRequisUpdateHandler(IBaseRepository<SplitRequis> SplitRequis)
        {
            this.SplitRequis = SplitRequis;
        }

        public async Task<Result> Handle(SplitRequisUpdateCommend request, CancellationToken cancellationToken)
        {
            var str = new Result() { Code = (int)CommonErrorCode.Update };

            try
            {
                var list = await SplitRequis.GetAll().Where(x => x.SplitRequisId == request.SplitRequisId).FirstAsync();
                if (list != null)
                {
                    list.SplitRequisId = request.SplitRequisId;
                    list.SplitRequisTime = request.SplitRequisTime;
                    list.DepotId = request.DepotId;
                    list.ProductId = request.ProductId;
                    list.MarkId = request.MarkId;
                    list.SplitRequisNum = request.SplitRequisNum;
                    list.SplitRequisGrossWeight = request.SplitRequisGrossWeight;
                    list.SplitRequisTares = request.SplitRequisTares;
                    list.SplitRequisWeight = request.SplitRequisWeight;
                    list.SplitRequisRemark = request.SplitRequisRemark;

                }
                var info = SplitRequis.Update(list);
                if (info != null)
                {
                    str.Code = (int)CommonErrorCode.Success;
                    str.Msg = "修改成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return str;
        }
    }
}
